<?php

namespace app\apisell\controller\admin;

use app\BaseController;
use app\common\utils\Res;
use think\facade\Db;

class PopuPanel extends BaseController
{
    /**
     * 数据面板
     */
    public function getPopuPanel()
    {
        $param = $this->request->param();
        //客户数
        $khs = array();
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $khs[] = ["add_time", '>', $param["add_time"][0]];
            $khs[] = ["add_time", '<=', $param["add_time"][1]];
        }else {
            $khs[] = ["add_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzkhs = Db::table('xh_sell_user')->field('id')->where($khs)->select()->toArray();
        $ljkhs = Db::table('xh_sell_user')->field('id')->select()->toArray();
        
        //新增成交客户数----------------------------------------------------------
        $xzcjid = array();
        foreach ($xzkhs as $key => $value) {
            $xzcjid[] = $value['id'];
        }
        $xzcjwhere[]  = array('user_id','in',$xzcjid);
        $xzcjwhere[]  = array('state','=',2);
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xzcjwhere[] = ["pay_time", '>', $param["add_time"][0]];
            $xzcjwhere[] = ["pay_time", '<=', $param["add_time"][1]];
        }else {
            $xzcjwhere[] = ["pay_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzcjkh = Db::table('xh_sell_order')->where($xzcjwhere)->count();
        
        $ljcjid = array();
        foreach ($ljkhs as $key1 => $value1) {
            $ljcjid[] = $value1['id'];
        }
        $ljcjwhere[]  = array('user_id','in',$ljcjid);
        $ljcjwhere[]  = array('state','=',2);
        $ljcjkh = Db::table('xh_sell_order')->where($ljcjwhere)->count();
        
        //新增客户成交率------------------------------------------------------------
        if ($xzcjkh > 0) {
            $xzkhcjl = ($xzcjkh / count($xzkhs)) * 100;
        }else{
            $xzkhcjl = 0;
        }
        if ($ljcjkh > 0) {
            $ljkhcjl = ($ljcjkh / count($ljkhs)) * 100;
        }else{
            $ljkhcjl = 0;
        }

        //新增推广员---------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xztgywhere[] = ["pass_time", '>', $param["add_time"][0]];
            $xztgywhere[] = ["pass_time", '<=', $param["add_time"][1]];
        }else {
            $xztgywhere[] = ["pass_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xztgywhere[] = ["status", '=', 1];
        $ljtgywhere[] = ["status", '=', 1];
        $xztgy = Db::table('xh_sell_popu_user')->where($xztgywhere)->count();
        $ljtgy = Db::table('xh_sell_popu_user')->where($ljtgywhere)->count();

        //新增一级推广员---------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xzyjwhere[] = ["pass_time", '>', $param["add_time"][0]];
            $xzyjwhere[] = ["pass_time", '<=', $param["add_time"][1]];
        }else {
            $xzyjwhere[] = ["pass_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzyjwhere[] = ["status", '=', 1];
        $xzyjwhere[] = ["popu_level", '=', 1];
        $ljyjwhere[] = ["status", '=', 1];
        $ljyjwhere[] = ["popu_level", '=', 1];
        $xzyjtgy = Db::table('xh_sell_popu_user')->where($xzyjwhere)->count();
        $ljyjtgy = Db::table('xh_sell_popu_user')->where($ljyjwhere)->count();

        //新增二级推广员---------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xzejwhere[] = ["pass_time", '>', $param["add_time"][0]];
            $xzejwhere[] = ["pass_time", '<=', $param["add_time"][1]];
        }else {
            $xzejwhere[] = ["pass_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzejwhere[] = ["status", '=', 1];
        $xzejwhere[] = ["popu_level", '=', 2];
        $ljejwhere[] = ["status", '=', 1];
        $ljejwhere[] = ["popu_level", '=', 2];
        $xzejtgy = Db::table('xh_sell_popu_user')->where($xzejwhere)->count();
        $ljejtgy = Db::table('xh_sell_popu_user')->where($ljejwhere)->count();

        //成交金额-------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xzcjjewhere[] = ["pay_time", '>', $param["add_time"][0]];
            $xzcjjewhere[] = ["pay_time", '<=', $param["add_time"][1]];
        }else {
            $xzcjjewhere[] = ["pay_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzcjjewhere[] = ["state", '=', 2];
        $cjorder = Db::table('xh_sell_order')->where($xzcjjewhere)->select()->toArray();
        $cjcount = array_column($cjorder, 'payer_total');
        // 计算金额总和
        $xzcjje = array_sum($cjcount);
        
        $ljcjjewhere[] = ["state", '=', 2];
        $ljcjorder = Db::table('xh_sell_order')->where($ljcjjewhere)->select()->toArray();
        $ljcjcount = array_column($ljcjorder, 'payer_total');
        // 计算金额总和
        $ljcjje = array_sum($ljcjcount);

        //成交订单-------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xzcjddwhere[] = ["pay_time", '>', $param["add_time"][0]];
            $xzcjddwhere[] = ["pay_time", '<=', $param["add_time"][1]];
        }else {
            $xzcjddwhere[] = ["pay_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xzcjddwhere[] = ["state", '=', 2];
        $xzcjdd = Db::table('xh_sell_order')->where($xzcjddwhere)->count();

        $ljcjddwhere[] = ["state", '=', 2];
        $ljcjdd = Db::table('xh_sell_order')->where($ljcjddwhere)->count();

        //退款金额-------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $xztkjewhere[] = ["r.agreetime", '>', $param["add_time"][0]];
            $xztkjewhere[] = ["r.agreetime", '<=', $param["add_time"][1]];
        }else {
            $xztkjewhere[] = ["r.agreetime", 'like' , '%' . $param["default_time"] . '%'];
        }
        $xztkjewhere[] = ["o.state", '=', 7];
        $xztk = Db::table('xh_sell_order')->alias('o')
            ->field('r.*')
            ->leftJoin('xh_sell_refund r','o.id = r.order_id')
            ->where($xztkjewhere)
            ->select()->toArray();
        $tkcount = array_column($xztk, 'price');
        // 计算退款金额总和
        $xztkje = array_sum($tkcount);

        $ljtkjewhere[] = ["o.state", '=', 7];
        $ljtk = Db::table('xh_sell_order')->alias('o')
            ->field('r.*')
            ->leftJoin('xh_sell_refund r','o.id = r.order_id')
            ->where($ljtkjewhere)
            ->select()->toArray();
        $ljtkcount = array_column($ljtk, 'price');
        // 计算退款金额总和
        $ljtkje = array_sum($ljtkcount);

        //退款率----------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $tzddwhere[] = ["pay_time", '>', $param["add_time"][0]];
            $tzddwhere[] = ["pay_time", '<=', $param["add_time"][1]];
        }else {
            $tzddwhere[] = ["pay_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $tzddwhere[] = ["state", '<>', 1];
        $tzddwhere[] = ["state", '<>', 6];
        $tzdd = Db::table('xh_sell_order')->where($tzddwhere)->count();
        if ($tzdd > 0) {
            $ttkl = (count($xztk) / $tzdd) * 100;
        }else{
            $ttkl = 0;
        }
        
        $zddwhere[] = ["state", '<>', 1];
        $zddwhere[] = ["state", '<>', 6];
        $zdd = Db::table('xh_sell_order')->where($zddwhere)->count();
        if ($zdd > 0) {
            $ztkl = (count($ljtk) / $zdd) * 100;
        }else{
            $ztkl = 0;
        }

        //产生佣金----------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $yjwhere[] = ["o.pay_time", '>', $param["add_time"][0]];
            $yjwhere[] = ["o.pay_time", '<=', $param["add_time"][1]];
        }else {
            $yjwhere[] = ["o.pay_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $tcsyjdata = Db::table('xh_sell_order')->alias('o')
            ->field('g.*')
            ->leftJoin('xh_sell_order_goods g','o.id = g.order_id')
            ->where($yjwhere)
            ->select()
            ->toArray();
        $tcsyjcount = array_column($tcsyjdata, 'commission');
        // 计算佣金月总和
        $tcsyj = array_sum($tcsyjcount);

        $zcsyjdata = Db::table('xh_sell_order')->alias('o')
                ->field('g.*')
                ->leftJoin('xh_sell_order_goods g','o.id = g.order_id')
                ->where('o.pay_time is not null')
                ->select()
                ->toArray();
        $zcsyjcount = array_column($zcsyjdata, 'commission');
        // 计算佣金总和
        $zcsyj = array_sum($zcsyjcount);

        //提现金额----------------------------------------------------------------------
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $ttxjewhere[] = ["add_time", '>', $param["add_time"][0]];
            $ttxjewhere[] = ["add_time", '<=', $param["add_time"][1]];
        }else {
            $ttxjewhere[] = ["add_time", 'like' , '%' . $param["default_time"] . '%'];
        }
        $ttxjewhere[] = ["status", '=', 2];
        $ttxjedata = Db::table('xh_sell_user_pay')->where($ttxjewhere)->select()->toArray();
        $ttxjecount = array_column($ttxjedata, 'money');
        $ttxje = array_sum($ttxjecount);

        $ztxjewhere[] = ["status", '=', 2];
        $ztxjedata = Db::table('xh_sell_user_pay')->where($ztxjewhere)->select()->toArray();
        $ztxjecount = array_column($ztxjedata, 'money');
        $ztxje = array_sum($ztxjecount);
        $data = [
            'xzkhs' => count($xzkhs),
            'ljkhs' => count($ljkhs),
            'xzcjkh' => $xzcjkh,
            'ljcjkh' => $ljcjkh,
            'xzkhcjl' => round($xzkhcjl,2),
            'ljkhcjl' => round($ljkhcjl,2),
            'xztgy' => $xztgy,
            'ljtgy' => $ljtgy,
            'xzyjtgy' => $xzyjtgy,
            'ljyjtgy' => $ljyjtgy,
            'xzejtgy' => $xzejtgy,
            'ljejtgy' => $ljejtgy,
            'xzcjje' => round($xzcjje / 100,2),
            'ljcjje' => round($ljcjje / 100,2),
            'xzcjdd' => $xzcjdd,
            'ljcjdd' => $ljcjdd,
            'xztkje' => round($xztkje / 100,2),
            'ljtkje' => round($ljtkje / 100,2),
            'ttkl' => round($ttkl,2),
            'ztkl' => round($ztkl,2),
            'tcsyj' => $tcsyj,
            'zcsyj' => $zcsyj,
            'ttxje' => $ttxje,
            'ztxje' => $ztxje,
        ];
        return Res::ok($data);
    }
    
}